import { _decorator, Component, Node, randomRangeInt, director, find } from 'cc';
import { Bullet } from './Bullet';
import { EquitmentEnum } from './Equitment';
import { EventManager } from './EventManager';
import { EventType } from './EventType';
import { JoyStick } from './JoyStick';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('EquitmentManager')
export class EquitmentManager extends Component
{
    public static instance: EquitmentManager

    public Bag: Map<EquitmentEnum, number> = new Map<EquitmentEnum, number>()
    public EquitsSelect: Array<number> = new Array() // 选择顺序

    public PlayerController: PlayerController

    onLoad()
    {
        EquitmentManager.instance = this
        director.addPersistRootNode(this.node)
    }

    GetEquitmentCount(eEquitment: EquitmentEnum)
    {
        return this.Bag[eEquitment] || 0
    }

    SetEquitment(equits:Array<number>)
    {
        this.EquitsSelect = new Array()
        this.Bag.clear()
        for (let index = 0; index < equits.length; index++) {
            const nEquitID = equits[index];
            
            this.EquitsSelect.push(nEquitID)
            if (!this.Bag.has(nEquitID))
            {
                this.Bag[nEquitID] = 1
            }
            else
            {
                this.Bag[nEquitID] += 1
            }
        }
    }

    GetAllEquitment()
    {
        return this.EquitsSelect
    }

    RandomEquitment(count: number = 3)
    {
        let e: Array<number> = new Array()
        for (let index = 0; index < count; index++)
        {
            let r = randomRangeInt(0, EquitmentEnum.MAX)
            e.push(r)
        }
        return e
    }

    OnSelectEquit(nEquitID)
    {
        switch (nEquitID as EquitmentEnum)
        {
            case EquitmentEnum.Armor:
                console.log("[EquitmentManager] HP+5")
                this.PlayerController.IncreaseHPMax(5)
                break;
            case EquitmentEnum.Shoe:
                console.log("[EquitmentManager] PLAYER_MOVE_SPEED + 1")
                JoyStick.PLAYER_MOVE_SPEED += 1
                break;
            case EquitmentEnum.Weapon:
                console.log("[EquitmentManager] BULLET_DAMAGE + 1")
                Bullet.BULLET_DAMAGE += 1
                break;

            default:
                console.error("[EquitmentManager] 未知装备ID：", nEquitID)
                return
                break;
        }
        this.EquitsSelect.push(nEquitID)
        if (!this.Bag.has(nEquitID))
        {
            this.Bag[nEquitID] = 1
        }
        else
        {
            this.Bag[nEquitID] += 1
        }

    }

    OnGameStart()
    {
        EventManager.instance.RegisterEvent(EventType.EquitmentSelect, this.OnSelectEquit, EquitmentManager.instance)
        this.PlayerController = find("Canvas/Player").getComponent(PlayerController)
    }

    onDestroy()
    {
        EventManager.instance.DeleteEvent(EventType.EquitmentSelect, this.OnSelectEquit, this)
    }
}


